/*
* Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.sample.gwtguestbook.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import java.util.ArrayList;
import java.util.List;
/**
* Guest signature widget class, containing the components needed to sign the
* guestbook.
*/
public class GuestSignaturePanel extends Composite implements
FiresEntryUpdates {
private HorizontalPanel guestSignaturePanel;
private TextBox guestNameBox;
private Label guestNameLabel;
private TextBox guestMessageBox;
private Label guestMessageLabel;
private Button signButton;
private boolean guestSignatureError = false;
/* Register for entry update handlers listening for guest signatures */
private List<EntryUpdateHandler> entryUpdateHandlers;
/**
* Creates a guest signature panel, with components fully instantiated and
* attached.
*
* The {@link com.google.gwt.user.client.ui.Composite#initWidget(Widget)}
* method must be called exactly once in the constructor to declare the
* widget that this composite class is wrapping.
*/
public GuestSignaturePanel() {
entryUpdateHandlers = new ArrayList<EntryUpdateHandler>();
guestSignaturePanel = new HorizontalPanel();
// Create guest signature panel widgets
guestNameBox = new TextBox();
guestMessageBox = new TextBox();
guestNameLabel = new Label("Name:");
guestMessageLabel = new Label("Message:");
signButton = new Button("Sign!");
// Set up the widget guest signature panel widget styles (additional to
// standard styles)
guestNameLabel.addStyleName("gb-Label");
guestMessageLabel.addStyleName("gb-Label");
guestNameBox.addStyleName("gb-NameBox");
guestMessageBox.addStyleName("gb-MessageBox");
// Listen for keyboard events in the input box.
guestMessageBox.addKeyPressHandler(new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event) {
if (event.getCharCode() == KeyCodes.KEY_ENTER) {
sign();
}
}
});
// Attach components together
guestSignaturePanel.add(guestNameLabel);
guestSignaturePanel.add(guestNameBox);
guestSignaturePanel.add(guestMessageLabel);
guestSignaturePanel.add(guestMessageBox);
guestSignaturePanel.add(signButton);
signButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
sign();
}
});
initWidget(guestSignaturePanel);
}
/**
* Listens for the guest entry update event, and persists the guest entry
*/
@Override
public void sign() {
if ("".equals(guestNameBox.getText())
|| "".equals(guestMessageBox.equals(""))) {
guestSignatureError = true;
fireUpdateError("Please make sure you've filled in all fields");
} else {
// Make the entry
GuestbookEntryTransferObject entry =
new GuestbookEntryTransferObject(
guestNameBox.getText(), guestMessageBox.getText());
GuestServiceAsync guestService =
(GuestServiceAsync) GWT.create(GuestService.class);
guestService.addGuestEntry(entry,
new AsyncCallback<List<GuestbookEntryTransferObject>>() {
@Override
public void onFailure(Throwable caught) {
fireUpdateError("Failed to add new entry to guest book");
}
@Override
public void onSuccess(List<GuestbookEntryTransferObject> result) {
Window.alert("Data has been sent");
guestNameBox.setText("");
guestMessageBox.setText("");
// Fire an update
fireUpdate(result);
}
});
}
}
/**
* Fires an update error and passes the error message to all registered update
* handlers
*
* @param errorMessage the error message to pass on
*/
protected void fireUpdateError(String errorMessage) {
for (EntryUpdateHandler handler : entryUpdateHandlers) {
handler.updateError(errorMessage);
}
}
/**
* Fires an update and passes the entries to all registered update handlers
*
* @param entries the latest guest entries to be displayed
*/
protected void fireUpdate(List<GuestbookEntryTransferObject> entries) {
for (EntryUpdateHandler handler : entryUpdateHandlers) {
// Signal that any previous sign in errors are now cleared to all
// registered handlers
if (guestSignatureError) {
handler.clearError();
guestSignatureError = false;
}
handler.updateEntries(entries);
}
}
@Override
public void addEntryUpdateHandler(EntryUpdateHandler handler) {
entryUpdateHandlers.add(handler);
}
@Override
public void removeEntryUpdateHandler(EntryUpdateHandler handler) {
entryUpdateHandlers.remove(handler);
}
}